function Population = improveWorstSubpopulation(Population, indicesToImprove, frequentPatterns, params)
    % Modifies chromosomes of the specified indices based on frequent patterns.
    % This is a complex step, simplified here.
    
    if isempty(frequentPatterns) || isempty(indicesToImprove)
        return;
    end
    
    fprintf('Applying frequent pattern improvements to %d individuals...\n', length(indicesToImprove));
    
    M = params.M;

    for idx = indicesToImprove(:)' % Iterate through indices to improve
        if idx > length(Population) || isempty(Population{idx}); continue; end
        
        chromo = Population{idx};
        num_modules_orig = size(chromo, 1);
        modified = false; % Flag if chromosome was changed

        % Randomly pick one frequent pattern to try and enforce
        pattern = frequentPatterns{randi(length(frequentPatterns))};
        pattern_elements = sort(pattern); % Ensure sorted order

        % Check if the pattern elements are currently split across modules
        modules_containing_pattern = [];
        for el = pattern_elements
            mod_idx = find(chromo(:, el), 1);
            if ~isempty(mod_idx)
                modules_containing_pattern = [modules_containing_pattern, mod_idx];
            end
        end
        unique_modules = unique(modules_containing_pattern);

        if length(unique_modules) > 1 % Pattern is split, try to merge
            target_module = unique_modules(1); % Choose the first module as target

            % Move all pattern elements to the target module
            for el = pattern_elements
                current_module = find(chromo(:, el), 1);
                 if ~isempty(current_module) && current_module ~= target_module
                     chromo(current_module, el) = 0; % Remove from old module
                     chromo(target_module, el) = 1; % Add to target module
                     modified = true;
                 elseif isempty(current_module) % Element wasn't assigned? Fix it.
                     chromo(target_module, el) = 1; 
                     modified = true;
                 end
            end
            
            % Remove empty rows potentially created by moving elements
            empty_rows = find(sum(chromo, 2) == 0);
            if ~isempty(empty_rows)
                chromo(empty_rows, :) = [];
                modified = true; 
            end
            
            % Check if any columns became all zero (shouldn't happen ideally)
            if ~all(sum(chromo,1) > 0)
                 warning('Improvement led to unassigned element for individual %d. Reverting.', idx);
                 chromo = Population{idx}; % Revert if invalid
                 modified = false;
            elseif size(chromo, 1) == 0 % Check if all rows removed somehow
                 warning('Improvement removed all modules for individual %d. Reverting.', idx);
                 chromo = Population{idx}; % Revert if invalid
                 modified = false;
            end


        end % End if pattern is split

        if modified
            Population{idx} = chromo; % Update the population
        end

    end % End loop through indicesToImprove
end
